<template>
  <div class="infoTable-containner">
    <div class="tableSearch">
      <el-form :inline="true"  class="demo-form-inline"  size="mini" >
        <el-form-item label="开始时间:">
          <el-date-picker
            v-model="model.starttime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="startTime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间:">
          <el-date-picker
            v-model="model.endtime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="endTime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="this.timeSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div  class="tableList">
      <el-table
        :data="tableData"
        border
        stripe border fit highlight-current-row
        style="width: 100%;height: 521px; max-height:521px;overflow-y: auto "
        :height='521'
        :show-overflow-tooltip="true"
        :header-cell-style="{background:'#D8DEE8'}"
      >
        <el-table-column
          prop="index"
          label="序号"
          align="center"
          width="60px"
        >
        </el-table-column>
        <el-table-column
          v-for="(column, index) in cols"
          :prop="column.prop"
          :label="column.label"
          :key="index"
          :show-overflow-tooltip="true"
          :width="column.width">
        </el-table-column>
      </el-table>
      <div class="pagination-container footerPage">
        <el-pagination
          @current-change="CurrentChange"
          @size-change="SizeChange"
          :page-sizes="[5, 10, 20, 50]"
          background layout="total, sizes, prev, pager, next"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
  import axios from 'axios'

  export default {
    props: {
      getResourceId: ''
    },
    data() {
      return {
        // 验证时间逻辑
        startTime: {
          disabledDate: time => {
            if (this.endtime) {
              return (
                time.getTime() > new Date(this.endtime).getTime()
              )
            } else {
              return time.getTime() > Date.now()
            }
          }
        },
        endTime: {
          disabledDate: time => {
            if (this.model.starttime) {
              return (
                time.getTime() > Date.now() ||
                time.getTime() < new Date(this.model.starttime).getTime()
              )
            } else {
              return time.getTime() > Date.now()
            }
          }
        },
        // 时间
        model: {
          starttime: '',
          endtime: ''
        },
        cols: [
          { prop: 'rid', label: '站点名称', width: '280px' },
          { prop: 'equip_name', label: '设备名称', width: '300px' },
          { prop: 'name', label: '测点名称', width: '310px', sortable: true },
          { prop: 'value', label: '测点值' },
          { prop: 'occurrence_time', label: '时间', width: '220px', sortable: true }
        ],
        tableData: [],
        indexMethod: '',
        total: 0,
        // 换页
        siteListQuery: {
          page: 1,
          limit: 20,
          importance: undefined,
          title: undefined,
          type: undefined,
          sort: '+id'
        },
        // 以下为接口
        submitdata: {
          access_token: window.localStorage.getItem('access_token'),
          type: 'real_history_get',
          pid: this.$route.query.pid,
          data: {
            condition: {
              begin_time: '2018-06-08 15:08:13',
              end_time: '2018-10-14 15:08:07',
              ids: []
            },
            page: {
              page_size: '10',
              page_number: '1'
            }
          }
        }
      }
    },
    mounted() {
      // this.dataSearchData()
    },
    methods: {
      // 警告
      alertMassage() {
        if ((this.submitdata.data.condition.begin_time === this.model.starttime && this.submitdata.data.condition.begin_time !== '') || (this.submitdata.data.condition.end_time === this.model.endtime && this.submitdata.data.condition.end_time !== '')) {
          this.dataSearchData()
        } else {
          this.$message({
            type: 'info',
            message: '请选择查询时间段，时间是查询的必须条件'
          })
        }
      },
      // 调取时间方法
      drawLine() {
        var strMouth = new Date().getMonth() + 1
        if (strMouth > 0 && strMouth < 10) {
          strMouth = '0' + strMouth
        }
        var strDate = new Date().getDate()
        if (strDate > 0 && strDate < 10) {
          strDate = '0' + strDate
        }
        var strMinutes = new Date().getMinutes()
        if (strMinutes > 0 && strMinutes < 10) {
          strMinutes = '0' + strMinutes
        }
        var strSeconds = new Date().getSeconds()
        if (strSeconds > 0 && strSeconds < 10) {
          strSeconds = '0' + strSeconds
        }
        this.model.starttime = (new Date().getFullYear() + '-' + strMouth + '-' + strDate + ' ' + '00' + ':' + '00' + ':' + '00')
        this.model.endtime = (new Date().getFullYear() + '-' + strMouth + '-' + strDate + ' ' + new Date().getHours() + ':' + strMinutes + ':' + strSeconds)
        this.submitdata.data.condition.begin_time = this.model.starttime
        this.submitdata.data.condition.end_time = this.model.endtime
      },
      // 换页
      handleSizeChange(val) {
        this.siteListQuery.limit = val
        this.gettableData()
      },
      handleCurrentChange(val) {
        this.siteListQuery.page = val
        this.tableData()
      },
      // 查询时间
      timeSearch() {
        this.submitdata.data.condition.begin_time = this.model.starttime
        this.submitdata.data.condition.end_time = this.model.endtime
        this.submitdata.data.condition.ids = this.getResourceId
        this.alertMassage()
        console.log(this.model.starttime)
        console.log(this.model.endtime)
      },
      // 以下都是接口获取的方法
      // 获取列表信息
      dataSearchData() {
        /* 有登录后可调整为以下代码**/
        /*
        updateRecord(this.submitdata).then(res => {
          console.log(11111111111111)
          console.log(res)
          this.$router.push({
            // path: 'usermanage'
          })
        }).catch(() => {
          console.log('失败!')
          return
        })*/
        // 信息查询 历史事件总览
        axios.post('/reports', this.submitdata, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.tableData = res.data.data.items
          for (let a = 0; a < res.data.data.items.length; a++) {
            this.tableData[a].index = (a + 1) + (res.data.data.page.page_number - 1) * res.data.data.page.page_size
          }
          this.total = parseInt(res.data.data.page.total)
          console.log(this.tableData)
          console.log(this.total)
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
            if ((error.data.errmsg === '查询必须的数据不能为空' && this.model.starttime === null) || (error.data.errmsg === '查询必须的数据不能为空' && this.model.starttime === '')) {
              this.$message({
                type: 'info',
                message: '请选择开始时间'
              })
            } else if ((error.data.errmsg === '查询必须的数据不能为空' && this.model.endtime === null) || (error.data.errmsg === '查询必须的数据不能为空' && this.model.endtime === '')) {
              this.$message({
                type: 'info',
                message: '请选择结束时间'
              })
            }
          })
      },
      // 当前页
      CurrentChange(val) {
        this.submitdata.data.page.page_number = (val) + ''
        this.dataSearchData()
      },
      // 每页显示多少条
      SizeChange(pageSize) {
        this.submitdata.data.page.page_size = (pageSize) + ''
        this.dataSearchData()
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .infoTable-containner{
    width: 100%;
    min-height: 500px;
    .tableSearch{
      width: 100%;
      min-height: 50px;
      background-color: #F0F3F5;
      font-size:14px;
      font-family:MicrosoftYaHei;
      font-weight:400;
      color:rgba(105,114,126,1);
      line-height: 20px;
      padding: 15px 20px  0px 20px;
      .el-input {
        position: relative;
        font-size: 14px;
        display: inline-block;
        width: 100%;
        /*padding-top: 10px;*/
      }


    }
    .tableList{
      width: 100%;
      max-height:616px;
    }
    .footerPage{
      float: right;
      right:20px;
      position: relative;
    }


  }
  /deep/.el-table--striped .el-table__body tr.el-table__row--striped td {
    background: #FAFAFA;
  }
  /deep/  .el-form--inline .el-form-item__content {
    display: inline-block;
    vertical-align: middle;
  }
  /deep/ .el-table--border th, .el-table__fixed-right-patch{
    padding: 8px 0;
  }
</style>
